import 'package:e_reception_flutter/colors/colors.dart';
import 'package:e_reception_flutter/common_widgets/common_fixed_height_confirm_button_widget/common_fixed_height_confirm_button_widget.dart';
import 'package:e_reception_flutter/constants/constant.dart';
import 'package:e_reception_flutter/singleton/theme_repository/theme_repository.dart';
import 'package:e_reception_flutter/utils/router_utils.dart';
import 'package:e_reception_flutter/utils/vg_dialog_utils.dart';
import 'package:e_reception_flutter/vg_widgets/vg_text_field_widget/vg_text_field.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:vg_base/vg_arch_lib.dart';
import 'package:vg_base/vg_string_util_lib.dart';

import '../test_function_view_model.dart';

class InputIdDialog extends StatefulWidget {

  final String id;
  final String title;
  final Function(String id) onConfirm;

  const InputIdDialog({Key key,this.id, this.title, this.onConfirm,})
      : super(key: key);

  @override
  _InputIdDialogState createState() =>
      _InputIdDialogState();

  ///跳转方法
  static Future navigatorPushDialog(BuildContext context,
      {String id, String title, Function onConfirm,}) {
    return VgDialogUtils.showCommonBottomDialog(
        context: context,
        child: InputIdDialog(
          id: id,
          title: title,
          onConfirm: onConfirm,
        )
    );
  }
}

class _InputIdDialogState
    extends BaseState<InputIdDialog> {
  TextEditingController _idController;
  bool isAlive = false;
  TestFunctionViewModel _viewModel;
  String _originId;
  @override
  void initState() {
    super.initState();
    _viewModel = TestFunctionViewModel(this);
    _idController = TextEditingController();
    if(StringUtils.isNotEmpty(widget?.id)){
      _idController?.text = widget?.id;
      _originId = widget?.id;
    }
    isAlive = judgeAlive();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedPadding(
      padding: MediaQuery.of(context).viewInsets, //边距（必要）
      duration: const Duration(milliseconds: 100), //时常 （必要）
      child: Container(
          decoration: BoxDecoration(
              color: ThemeRepository.getInstance().getCardBgColor_21263C(),
              borderRadius:
                  BorderRadius.circular(DEFAULT_BOTTOM_CARD_DIALOG_RADIUS)),
          child: _toMainColumnWidget()),
    );
  }

  Widget _toMainColumnWidget() {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        Row(
          children: [
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 15,),
              child: Text(
                StringUtils.isNotEmpty(widget?.title??"")?widget?.title:"机身侧面编号",
                style: TextStyle(
                    fontSize: 17,
                    color: ThemeRepository.getInstance()
                        .getTextMainColor_D0E0F7(),fontWeight: FontWeight.w600
                ),
              ),
            ),
            Spacer(),
            _buttonWidget(),
            SizedBox(width: 16,)
          ],
        ),
        Container(
          margin: EdgeInsets.symmetric(horizontal: 15),
          height: 40,
          child: Row(
            children: [
              Expanded(
                child: Container(
                  decoration: BoxDecoration(
                    color:
                    ThemeRepository.getInstance().getBgOrSplitColor_191E31(),
                    borderRadius: BorderRadius.circular(8)
                  ),
                  child: VgTextField(
                    controller: _idController,
                    maxLines: 1,
                    minLines: 1,
                    maxLength: 30,
                    autofocus: true,
                    decoration: InputDecoration(
                      border: InputBorder.none,
                      counterText: "",
                      contentPadding: const EdgeInsets.only(left: 15, bottom: 10),
                      hintText: "请输入",
                      hintStyle: TextStyle(
                          color: ThemeRepository.getInstance()
                              .getTextEditHintColor_3A3F50(),
                          fontSize: 14),
                    ),
                    style: TextStyle(
                        fontSize: 14,
                        color: ThemeRepository.getInstance()
                            .getTextMainColor_D0E0F7()),
                    onChanged: (String str) {
                      isAlive = judgeAlive();
                      setState(() {});
                    },
                  ),
                ),
              ),
            ],
          ),
        ),
        SizedBox(height: 20,),
      ],
    );
  }

  Widget _buttonWidget() {
    return Container(
      child: CommonFixedHeightConfirmButtonWidget(
        isAlive: isAlive ?? false,
        width: 76,
        height: 30,
        unSelectBgColor:
            ThemeRepository.getInstance().getTextEditHintColor_3A3F50(),
        selectedBgColor: ThemeRepository.getInstance().getPrimaryColor_1890FF(),
        unSelectTextStyle: TextStyle(
          color: VgColors.INPUT_BG_COLOR,
          fontSize: 14,
        ),
        selectedTextStyle: TextStyle(
          color: Colors.white,
          fontSize: 14,
          height: 1.2,
        ),
        text: "确定",
        onTap: () {
          if (isAlive) {
            _viewModel.checkId(context, _idController?.text, callback: (success){
              if(success){
                // RouterUtils.pop(context);
                widget?.onConfirm?.call(_idController?.text);
              }
            });
          }
        },
      ),
    );
  }


  bool judgeAlive(){
    return StringUtils.isNotEmpty(_idController.text) && _idController.text != _originId;
  }
}
